<script lang="ts" setup>
import type { VxeGridProps } from '#/adapter/vxe-table';
import { Page,useVbenDrawer } from '@vben/common-ui';
import { Button,Switch,Popconfirm,message } from 'ant-design-vue';
import { IconifyIcon } from '@vben/icons';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getListApi,deleteApi,setstatusApi,editApi } from '#/api/user/grade';
import Form from './form.vue';

interface RowType {
  grade_id: number;
  name: string;
  color: string;
  weight: number;
  upgrade: { points: number };
  equity: { discount: number };
  sort: number;
  old_sort: number;
  status: number;
  create_time: string;
  update_time: string;
}

const gridOptions: VxeGridProps<RowType> = {
  columns: [
    { field: 'grade_id', title: 'ID', minWidth:'auto' },
    { field: 'name', slots: { default: 'name' }, title: '名称', minWidth:'auto' },
    { field: 'weight', title: '权重', minWidth:'auto' },
    { field: 'upgrade', slots: { default: 'upgrade' }, title: '升级条件', minWidth:'auto' },
    { field: 'equity', slots: { default: 'equity' }, title: '权益', minWidth:'auto' },
    { field: 'status', slots: { default: 'status' }, title: '状态', minWidth:'auto' },
    { field: 'sort', title: '排序', editRender: { name: 'VxeNumberInput', props: { type: 'integer' } }, minWidth:'auto' },
    { field: 'create_time', title: '创建时间', minWidth:'auto' },
    { field: 'action', fixed: 'right', slots: { default: 'action' }, title: '操作', minWidth:'auto' },
  ],
  editConfig: {
    mode: 'cell',
    trigger: 'click',
  },
  showOverflow: false,
  exportConfig: {},
  keepSource: true,
  // border: true,
  proxyConfig: {
    ajax: {
      query: async ({ page }) => {
        return await getListApi({
            page: page.currentPage,
            pageSize: page.pageSize
        }).then((res) => {
          res.items.forEach((item: any) => {
            item.old_sort = item.sort;
          });
          return res;
        });
      },
    },
    sort: true,
  },
  toolbarConfig: {
    custom: true,
    // export: true,
    // import: true,
    refresh: { code: 'query' },
    zoom: true,
  },
  rowConfig: {
    keyField: 'grade_id',//主键的字段名
  },
};

const [Grid, gridApi] = useVbenVxeGrid({ gridOptions,gridEvents:{
  editClosed: (params : any) => {
    if(params.row.sort != params.row.old_sort){
      editApi({grade_id:params.row.grade_id,sort:params.row.sort,weight:params.row.weight}).then((res) => {
        message.success({content: res.message  ?? '操作成功'});
        gridApi.query();
      })
    }
  }
} });

const [FormDrawer, formDrawerApi] = useVbenDrawer({
  connectedComponent: Form,
  onBeforeClose() {
    gridApi.query();
    return true;
  },
});
function openFormDrawer(item={grade_id:0}) {
  formDrawerApi
    .setData(item)
    .open();
}
function confirmDel(id: number) {
  gridApi.setLoading(true);
  deleteApi(id).then((res) => {
    gridApi.setLoading(false);
    message.success({
      content: res.message,
    });
    gridApi.query();
  });
}
function setStatus(id: number,status: number) {
  setstatusApi({id,status}).then((res) => {
    message.success({
      content: res.message,
    });
  });
}
</script>

<template>
  <Page>
    <Grid table-title="等级列表">
      <template #toolbar-tools>
        <Button class="mr-2" type="primary" shape="round" @click="openFormDrawer()"> <div class="flex items-center justify-center"><IconifyIcon class="size-4 cursor-pointer outline-none mr-1" icon="ant-design:plus-circle-filled" />新增等级</div> </Button>
      </template>
      <template #name="{ row }">
        <span :style="{color:row.color}">{{ row.name }}</span>
      </template>
      <template #upgrade="{ row }">
        <span>积分:{{ row.upgrade.points }}</span>
      </template>
      <template #equity="{ row }">
        <span>折扣:{{ row.equity.discount }}</span>
      </template>
      <template #status="{ row }">
        <Switch v-model:checked="row.status" @change="setStatus(row.grade_id,row.status)" checkedChildren="正常" :checkedValue="1" unCheckedChildren="禁用" :unCheckedValue="0" />
      </template>
      <template #action="{ row }">
        <Button type="link" class="px-2" @click="openFormDrawer(row)"><IconifyIcon class="size-4 cursor-pointer outline-none" icon="ant-design:edit-outlined" /></Button>
        <Button type="link" class="px-0 mx-2" danger>
          <popconfirm title="确认删除此等级?" ok-text="确认" cancel-text="取消" placement="left" @confirm="confirmDel(row.grade_id)">
            <template #icon>
              <IconifyIcon class="size-4 text-red-600 outline-none inline" icon="ant-design:question-circle-outlined" />
            </template>
            <IconifyIcon class="size-4 cursor-pointer outline-none" icon="ant-design:delete-outlined" />
          </popconfirm>
        </Button>
      </template>
    </Grid>
    <FormDrawer />
  </Page>
</template>
